package com.zttek.photosShareProject.modules.service.manager.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.zttek.photosShareProject.common.keyword.SensitivewordFilter;
import com.zttek.photosShareProject.modules.mapper.KeywordMapper;
import com.zttek.photosShareProject.modules.model.Keyword;
import com.zttek.photosShareProject.modules.service.manager.IKeywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * Created by 13296 on 2017/8/15.
 */
@Service
public class KeywordServiceImpl implements IKeywordService {
    @Autowired
    private KeywordMapper keywordMapper;

    /**
     * 查询所有关键字
     * @return
     * @throws Exception
     */
    @Override
    public List<Keyword> selectAllKeyword() throws Exception {
        EntityWrapper<Keyword> ew = new EntityWrapper<>();
        ew.where("valid=1");
        List<Keyword> list = keywordMapper.selectList(ew);
        return list;
    }

    /**
     * 分页查询关键字
     * @param page
     * @return
     * @throws Exception
     */
    @Override
    public Page<Keyword> selectKeywordPage(Page<Keyword> page) throws Exception {
        EntityWrapper<Keyword> ew = new EntityWrapper<>();
        ew.where("valid=1").orderBy("createTime",false).orderBy("id");
        page.setRecords(keywordMapper.selectPage(page,ew));
        return page;
    }

    /**
     * 根据关键字类型分页查询
     * @param page
     * @param kwType
     * @return
     * @throws Exception
     */
    @Override
    public Page<Keyword> selectKeywordPageByKwType(Page<Keyword> page, String kwType) throws Exception {
        EntityWrapper<Keyword> ew = new EntityWrapper<>();
        ew.where("valid=1").and("kwType={0}",kwType)
        .orderBy("createTime").orderBy("id");
        page.setRecords(keywordMapper.selectPage(page,ew));
        return page;
    }

    /**
     * 查询所有的关键字并保存在Set中
     * @return
     * @throws Exception
     */
    @Override
    public Set<String> selectAllKeywordSet() throws Exception {
        Set<String> set = new HashSet<>();
        EntityWrapper<Keyword> ew = new EntityWrapper<>();
        ew.where("valid=1");
        List<Keyword> list = keywordMapper.selectList(ew);
        for(Keyword kw:list){
            set.add(kw.getKeyword());
        }
        return set;
    }

       /**
     * 根据分享文字返回关键字匹配结果,匹配到的关键字保存在Set中
     * @param description 分享文字内容
     * @return
     * @throws Exception
     */
    @Override
    public Set<String> getContainKeywordSet(String description)throws Exception{
        Set<String> set = this.selectAllKeywordSet();
        SensitivewordFilter filter = new SensitivewordFilter(set);
        Set<String> ResultSet = filter.getSensitiveWord(description, 1);
        System.out.println("匹配结果"+ResultSet);
        return ResultSet;
    }

    /**
     * 根据id查询关键字
     * @param id
     * @return
     * @throws Exception
     */
    @Override
    public Keyword selectKeywordById(Long id) throws Exception {
        Keyword keyword = keywordMapper.selectById(id);
        return keyword;
    }

    /**
     * 模糊搜索关键字
     * @param page
     * @param value
     * @return
     * @throws Exception
     */
    @Override
    public Page<Keyword>  selectLike(Page<Keyword> page,String value) throws Exception {
        EntityWrapper<Keyword> ew = new EntityWrapper<>();
        //ew.setEntity(new Keyword());
        if (value.trim().length()==0&&value.trim().equals("")){
            ew.where("valid=1").orderBy("createTime");
        }else{
            System.out.println("FeiKong");
            ew.where("valid=1").like("keyword",value)
                    .or().like("kw_type",value).orderBy("createTime",false);
        }
        page.setRecords(keywordMapper.selectPage(page,ew));
        return page;
    }

    /**
     * 插入关键字
     * @param keyword
     * @throws Exception
     */
    @Override
    @Transactional
    public void insertKeyword(Keyword keyword) throws Exception {
        keywordMapper.insert(keyword);
    }

    /**
     * 删除关键字
     * @param id
     * @throws Exception
     */
    @Override
    @Transactional
    public void deleteKeywordById(Integer id) throws Exception {
        keywordMapper.deleteById(id);
    }

    /**
     * 更新关键字
     * @param keyword
     * @return
     * @throws Exception
     */
    @Override
    @Transactional
    public Integer updateKeyword(Keyword keyword) throws Exception {
        Integer result = 0;
        result = keywordMapper.updateById(keyword);
        return result;
    }
}
